home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / etc / trie-gen / options.cc < prev    next >
C/C++ Source or Header  |  1992-03-07  |  5KB  |  207 lines

  1. /* Handles parsing the Options provided to the user.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  4.  
  5. This file is part of GNU TRIE-GEN.
  6.  
  7. GNU TRIE-GEN is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU TRIE-GEN is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU TRIE-GEN; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <builtin.h>
  23. #include <assert.h>
  24. #include <stdarg.h>
  25. #include <GetOpt.h>
  26. #include "options.h"
  27.  
  28. /* Global option coordinator for the entire program. */
  29. Options option;       
  30.  
  31. /* Current program version. */
  32. extern char *version_string;
  33.  
  34. static void
  35. report_error (const char *format, ...)
  36. {
  37.   va_list args;
  38.   va_start (args, format);
  39.   char c;
  40.  
  41.   while (c = *format++)
  42.     {
  43.       if (c=='%')
  44.         switch (c = *format++)
  45.           {
  46.           case 'a':
  47.             exit (1);
  48.           case 'n':
  49.             fputs (option.program_name (), stderr);
  50.             break;
  51.           case 'e':
  52.             typedef void (*CallbackT) (void);
  53.             CallbackT callback = va_arg (args, CallbackT);
  54.             (*callback) ();
  55.             break;
  56.           case 's':
  57.             fputs (va_arg (args, const char *), stderr);
  58.             break;
  59.           default:
  60.             fputc (c, stderr);
  61.           }
  62.       else
  63.         fputc (c, stderr);
  64.     }
  65.   va_end (args);
  66. }
  67.  
  68. /* Prints program usage to standard error stream. */
  69.  
  70. inline void 
  71. Options::usage (void) 
  72.   report_error ("Usage: %n [-cdf] (type %n -h for help)\n");
  73. }
  74.  
  75. /* Output command-line Options. */
  76.  
  77. void 
  78. Options::print_options (void)
  79.   int i;
  80.  
  81.   printf ("/* Command-line: ");
  82.  
  83.   for (i = 0; i < argument_count; i++) 
  84.     printf ("%s ", argument_vector[i]);
  85.    
  86.   printf (" */");
  87. }
  88. /* Sets the default Options. */
  89.  
  90. int     Options::option_word = 0; 
  91. int     Options::argument_count = 0;
  92. char ** Options::argument_vector = 0;
  93.  
  94. Options::Options (void) 
  95.   option_word = 0;
  96. }
  97.  
  98. /* Dumps option status when debug is set. */
  99.  
  100. Options::~Options (void) 
  101.   if (option_word & DEBUG)
  102.     {
  103.       fprintf (stderr, "\ndumping Options:\nCOMPACT is.......: %s\nDEBUG is.......: %s"
  104.                "\nFULL is........: %s\nCONST is.......: %s",
  105.                option_word & COMPACT ? "enabled" : "disabled",
  106.                option_word & DEBUG ? "enabled" : "disabled",
  107.                option_word & FULL ? "enabled" : "disabled",
  108.                option_word & CONST ? "enabled" : "disabled");
  109.  
  110.       fprintf (stderr, "\nfinished dumping Options\n");
  111.     }
  112. }
  113.  
  114.  
  115. /* Parses the command line Options and sets appropriate flags in option_word. */
  116.  
  117. void 
  118. Options::operator () (int argc, char *argv[])
  119.   GetOpt getopt (argc, argv, "cCdfh");
  120.   int    option_char;
  121.  
  122.   argument_count = argc;
  123.   argument_vector = argv;
  124.  
  125.   while ((option_char = getopt ()) != EOF)
  126.     {
  127.       switch (option_char)
  128.         {
  129.         case 'c':               /* Compact the output tables. */
  130.           {
  131.             option_word |= COMPACT;
  132.             break;
  133.           }
  134.         case 'C':               /* Make the generated tables readonly (const). */
  135.           {
  136.             option_word |= CONST;
  137.             break;
  138.           }
  139.         case 'd':               /* Enable debugging option. */
  140.           { 
  141.             option_word |= DEBUG;
  142.             report_error ("Starting program %n, version %s, with debuggin on.\n",
  143.                           version_string);
  144.             break;
  145.           }   
  146.         case 'f':               /* Generate a full table */
  147.           {
  148.             option_word |= FULL;
  149.             break;
  150.           }
  151.         case 'h':
  152.           {
  153.             report_error ("-c\tCompact the generated trie.\n"
  154.                           "-C\tMake strings in the generated lookup table constant, i.e., readonly.\n"
  155.                           "-d\tEnable debugging (produces verbose output to standard error).\n"
  156.                           "-f\tGenerates a `full' trie rather than a minimal-prefix trie.\n"
  157.                           "-h\tPrints out the help diagnostic.\n%a");
  158.           }
  159.         default: 
  160.           report_error ("%e%a", usage);
  161.         }
  162.     }
  163.   
  164.   if (argv[getopt.optind] && ! freopen (argv[getopt.optind], "r", stdin))
  165.     report_error ("Unable to read key word file %s.\n%e%a", argv[getopt.optind], usage);
  166.   
  167.   if (++getopt.optind < argc) 
  168.     report_error ("Extra trailing arguments to %n.\n%e%a", usage);
  169. }
  170.  
  171. #ifndef __OPTIMIZE__
  172. const char *
  173. Options::program_name (void)
  174. {
  175.   return argument_vector[0];
  176. }
  177.  
  178. /* TRUE if option enable, else FALSE. */
  179. int  
  180. Options::operator[] (Option_Type option) 
  181.   return option_word & option;
  182. }
  183.  
  184. /* Enables option OPT. */
  185. void
  186. Options::operator= (enum Option_Type opt) 
  187. {
  188.   option_word |= opt;
  189. }
  190.  
  191. /* Disables option OPT. */
  192. void
  193. Options::operator!= (enum Option_Type opt) 
  194. {
  195.   option_word &= ~opt;
  196. }
  197.  
  198. #endif /* not defined __OPTIMIZE__ */
  199.  
  200.  
  201.